package chatServer;

import chatUniversal.Message;
import chatUniversal.InterfNotifier;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author unknown
 */
public class ChatServiceRemoteImpl extends UnicastRemoteObject implements ChatServiceRemoteInterface {

    List<Message> latest;
    List<InterfNotifier> notifiers;
    
    /**
     * constructor initializes the 'latest messages' list
     * @throws RemoteException
     */
    public ChatServiceRemoteImpl() throws RemoteException {
        latest = new LinkedList<>();
        notifiers = new LinkedList<>();
    }
    
    @Override
    public void registerClient(String Client, String ipAddress) throws RemoteException {
        try {
            InterfNotifier notifier = (InterfNotifier)Naming.lookup("rmi://"+ipAddress+"/"+Client);
            notifiers.add(notifier);
        } catch (NotBoundException ex) {
            Logger.getLogger(ChatServiceRemoteImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ChatServiceRemoteImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    @Override
    public void removeClient(String Client) throws RemoteException {
        Iterator iter = notifiers.iterator();
        for(int x = 0; x < notifiers.size(); x++) {
            if(((InterfNotifier)iter.next()).getClient().equals(Client)) {
                iter.remove();
                return;
            }
        }
    }
    
    @Override
    /**
     * sendMessage adds a message to the latest messages list
     * @param message
     * @param fromClient
     * @return
     * @throws RemoteException
     */
    public String sendMessage(String message, String fromClient) throws RemoteException {
        latest.add(new Message(message, fromClient));

        Iterator iter = notifiers.iterator();
        for(int x = 0; x < notifiers.size(); x++) {
            ((InterfNotifier)iter.next()).retrieveMessages();
        }
        
        return message; //Let client know sending the message succeeded.
    }

    /**
     * getLatest fetches the latest messages from the list
     * @param client
     * @return
     */
    @Override
    public synchronized List<Message> getLatest(String client) {
        List<Message> temp = new LinkedList<>();

        Iterator iter = latest.iterator();
        for (int x = 0; x < latest.size(); x++) {

            Message message = (Message) iter.next();
            if (!client.equals(message.fromClient)) {
                temp.add(message);
                if(++message.read == notifiers.size() - 1) { //If all other clients have read the message
                    iter.remove();
                }
            }
        }

        return temp;
    }
}
